package com.springbootmybatiscurd.controller;

import com.springbootmybatiscurd.pojo.Dept;
import com.springbootmybatiscurd.pojo.Result;
import com.springbootmybatiscurd.service.deptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 部门管理
 * */
@Slf4j
@RestController
@CrossOrigin(origins = "*")   //设置跨域
@RequestMapping("/depts")
public class controllerDept {

    /**查询部门信息
     * */
   // private static Logger log= LoggerFactory.getLogger(DeptController.class);// 日志
  //  @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @Autowired
   private deptService deptService;

    @GetMapping
    public Result DeptController(){
        List<Dept> depts = deptService.DeptService();

        log.info("查询全部部门数据",depts);
        return Result.success(depts);
    }

   /**
    * 根据id删除部门
    * */
    @DeleteMapping("/{id}")
    public Result deptList(@PathVariable Integer id){
        Integer deptId = deptService.deleteId(id);

        log.info("删除部门",deptId);
        return Result.success();
    }
 /**
  * 添加部门
  * */
    @PostMapping()
    public Result addDept(@RequestBody Dept dept){
        System.out.println(dept);
       deptService.addDept(dept);
        return Result.success("0");
    }


    /**
     *
     * 根据id查询部门信息
     * */
    @GetMapping("/{id}")
    public Result deptSelect(@PathVariable Integer id){
        List<Dept> depts = deptService.selectId(id);

        log.info("查询部门",depts);
        return Result.success(depts);
    }


    /**
     * 修改部门
     * @return id,name
     * */
    @PutMapping
    public Result deptUpdate(@RequestBody Dept dept){
         deptService.deptUpdate(dept);

        log.info("修改部门");
        return Result.success("1");
    }
}
